package com.atguigu.tingshu.payment.service.impl;

import cn.hutool.core.lang.Assert;
import com.atguigu.tingshu.account.AccountFeignClient;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.model.account.RechargeInfo;
import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.model.payment.PaymentInfo;
import com.atguigu.tingshu.order.client.OrderFeignClient;
import com.atguigu.tingshu.payment.mapper.PaymentInfoMapper;
import com.atguigu.tingshu.payment.service.PaymentInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.swing.plaf.ProgressBarUI;

@Service
@SuppressWarnings({"all"})
public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, PaymentInfo> implements PaymentInfoService {

    @Autowired
    private OrderFeignClient orderFeignClient;

    @Autowired
    private AccountFeignClient accountFeignClient;

    /**
     * 保存本地交易记录
     *
     * @param paymentType:支付类型 1301-订单 1302-充值
     * @param orderNo:         订单编号
     * @return
     */
    @Override
    public PaymentInfo savePaymentInfo(String paymentType, String orderNo) {
        //1.根据订单编号查询本地交易记录，如果存在，则直接返回
        LambdaQueryWrapper<PaymentInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PaymentInfo::getOrderNo, orderNo);
        PaymentInfo paymentInfo = this.getOne(queryWrapper);
        if (paymentInfo != null) {
            return paymentInfo;
        }
        //2.封装本地交易记录对象完成保存
        //2.1 初始化本地交易对象
        paymentInfo = new PaymentInfo();
        //2.2 封装基本信息
        paymentInfo.setOrderNo(orderNo);
        paymentInfo.setPaymentType(paymentType);
        //支付状态：1401-未支付 1402-已支付
        paymentInfo.setPaymentStatus(SystemConstant.PAYMENT_STATUS_UNPAID);
        paymentInfo.setPayWay(SystemConstant.ORDER_PAY_WAY_WEIXIN);
        //2.3 判断支付类型-如果是订单，根据订单编号远程调用订单服务获取订单信息
        if (SystemConstant.PAYMENT_TYPE_ORDER.equals(paymentType)) {
            //2.3.1 远程调用订单服务获取订单信息
            OrderInfo orderInfo = orderFeignClient.getOrderInfo(orderNo).getData();
            Assert.notNull(orderInfo, "订单{}不存在", orderNo);
            String orderStatus = orderInfo.getOrderStatus();
            if (!SystemConstant.ORDER_STATUS_UNPAID.equals(orderStatus)) {
                throw new RuntimeException("订单状态有误");
            }
            //2.3.2 封装本地交易记录中内容跟金额
            paymentInfo.setUserId(orderInfo.getUserId());
            paymentInfo.setAmount(orderInfo.getOrderAmount());
            paymentInfo.setContent(orderInfo.getOrderTitle());
        }
        //2.4 判断支付类型-如果是充值，根据充值订单编号远程调用账户服务获取充值信息
        if (SystemConstant.PAYMENT_TYPE_RECHARGE.equals(paymentType)) {
            //2.4.1 远程调用账户服务获取充值信息
            RechargeInfo rechargeInfo = accountFeignClient.getRechargeInfo(orderNo).getData();
            Assert.notNull(rechargeInfo, "充值订单{}不存在", orderNo);
            //2.4.2 判断充值状态
            String rechargeStatus = rechargeInfo.getRechargeStatus();
            if (!SystemConstant.ORDER_STATUS_UNPAID.equals(rechargeStatus)) {
                throw new RuntimeException("充值订单状态有误");
            }
            //2.4.3 封装本地交易记录中内容跟金额
            paymentInfo.setUserId(rechargeInfo.getUserId());
            paymentInfo.setAmount(rechargeInfo.getRechargeAmount());
            paymentInfo.setContent("充值：" + rechargeInfo.getRechargeAmount());
        }
        // 微信端交易订单号，回调时间，回调内容待用户付款成功后，商户系统才会收到回调
        //paymentInfo.setOutTradeNo();
        //paymentInfo.setCallbackTime();
        //paymentInfo.setCallbackContent();

        //3.保存新增本地交易记录
        this.save(paymentInfo);
        return paymentInfo;
    }
}
